<?php

namespace cms\modules\v1\models\coin;

use cms\modules\common\models\Admin;
use cms\modules\v1\models\Common;
use cms\modules\v1\models\user\User;
use common\models\Functions;
use common\models\Status;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class CoinRechargeOrder extends \common\models\coin\CoinRechargeOrder
{
    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @param array $search
     * @return array
     */
    public function getListData($page = 1, $size = 20, $search = []) {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件
            //得到文章的总数
            $count = static::getObjectCountByAndWhere($searchWhere['where'], $searchWhere['andWhere'])->count();
            $data = static::setPageinationAPI($page, $size, $count);
            //设置分页
            $offset = ($page - 1) * $size;

            //排序 order
            $orderSql = 'pay_time DESC, id DESC';

            $query = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);

            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @return string
     */
    public function getSearch () {
        $where = [];
        $andWhere = [];
        $status = Yii::$app->request->get('status',0);
        $payWay = Yii::$app->request->get('pay_way',0);
        $orderNumber = Yii::$app->request->get('order_number',"");
        $startTime = Yii::$app->request->get('start',"");
        $endTime = Yii::$app->request->get('end',"");
        $userPhone = Yii::$app->request->get('user_phone',"");
        $where['is_delete'] = Status::COMMON_RADIO_YES;
        if($userPhone) {
            $userWhere[] = ['like','login_name',$userPhone];
            $userId = User::getFormArrayAndWhere([],$userWhere,'id','id');
            $where['user_id'] = $userId;
        }

        if($status) {
            $where['status'] = $status;
        }

        if($payWay) {
            $where['pay_way'] = $payWay;
        }

        if($orderNumber) {
            $where['order_number'] = $orderNumber;
        }

        if(!empty($startTime) && empty($endTime)) {
            $andWhere[] = ['>','create_time',$startTime];
        } elseif(empty($startTime) && !empty($endTime)) {
            $andWhere[] = ['<','create_time',$endTime];
        } elseif (!empty($startTime) && !empty($endTime)) {
            $andWhere[] = ['between','create_time',$startTime, $endTime];
        }
        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 处理初始化数据
     * @param $query
     * @return mixed
     */
    public function handelList($query) {
        if (empty($query)) {
            return null;
        }
        $list = $query->with([
            'user' => function ($q) {
                $q->select(['id','login_name','user_face','nick_name','tx_code','user_code']);
            },
            'setting' => function ($q) {
                $q->select(['id','name']);
            },
        ])->asArray()->all();
        foreach($list as $key => &$value) {
            $uerInfo = ArrayHelper::getValue($value,'user',[]);

            $settingInfo = ArrayHelper::getValue($value,'setting',[]);
            $value['user_face'] = static::getImages($uerInfo['user_face']);
            $value['user_phone'] = $uerInfo['login_name'] ?? '';
            $value['user_nick_name'] = $uerInfo['nick_name'] ?? '已注销';

            $value['setting'] = $settingInfo['name'] ?? '';
            $value['money'] = floatval($value['money']);
            $value['coin'] = floatval($value['coin']);
            $value['status_name'] = Status::COIN_RECHARGE_ORDER_STATUS_MAP[$value['status']] ?? '';
            $value['pay_way_name'] = Status::PAY_WAY_MAP[$value['pay_way']] ?? '';
        }
        return $list;
    }

    /**
     * 删除全部订单
     * @param int $id
     * @return array
     */
    public function saveDelOrder() {
        try {

            $res = static::updateData(['is_delete' => Status::COMMON_RADIO_NO]);
            if($res == false) {
                throw new Exception('操作失败');
            }

            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
